//
// Created by 卫志龙 on 2023/7/17.
//
#include "../common/common.h"

#define LIST_INIT_SIZE 100  //线性表初始大小
#define LIST_INCREMENET 10 //线性表扩容后增加的大小
#define ElementType int

int *p,*q;

typedef struct {
    ElementType *elem;
    int length;
    int list_size;
}SqList;

//初始化线性表
void InitList_Sq(SqList &L) {
    L.elem = (ElementType *) malloc(LIST_INIT_SIZE * sizeof (ElementType));
    if (!L.elem) exit(0);
    L.length = 0;
    L.list_size = LIST_INIT_SIZE;
}

/**
 * 线性表的插入
 * @param L 线性表
 * @param i 插入的位置，从1开始
 * @param e 插入的元素
 * @return
 */
Status ListInsert_Sq(SqList &L, int i, ElementType e) {
    if (i < 1 || i > L.length + 1) return ERROR;
    int* new_base;
    if (L.length >= L.list_size) {      //当前储存空间已满
        new_base = (ElementType *) realloc(L.elem, (L.list_size + LIST_INCREMENET) * sizeof (ElementType));
        if (!new_base) {
            exit(0);
        }
        L.elem = new_base;
        L.list_size += LIST_INCREMENET;
    }
    q = &(L.elem[i - 1]);
    for (p = &(L.elem[L.length - 1]); p >= q ; --p) {
        *(p + 1) = *p;
    }
    *q = e;
    ++L.length;
    return OK;
}

void printSqList(SqList &L) {
    printf("list:[");
    for (int i = 0 ; i < L.length ; i++) {
        printf("%d",L.elem[i]);
        if (i != L.length - 1) {
            printf(",");
        }
    }
    printf("]\n");
}

int main() {
    SqList list;
    InitList_Sq(list);
    ListInsert_Sq(list , 1 , 0);
    ListInsert_Sq(list , 2 , 2);
    printSqList(list);
    return 0;
}